package io.renren.modules.sys.controller;

import io.renren.common.annotation.SysLog;
import io.renren.common.exception.RRException;
import io.renren.common.utils.Constant;
import io.renren.common.utils.R;
import io.renren.modules.sys.entity.SysCollegeEntity;
import io.renren.modules.sys.entity.SysMenuEntity;
import io.renren.modules.sys.service.ShiroService;
import io.renren.modules.sys.service.SysCollegeService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 学院管理
 *
 * @author liao mengqing
 */
@RestController
@RequestMapping("sys/college")
public class SysCollegeController extends AbstractController {

    @Autowired
    private SysCollegeService sysCollegeService;

    /**
     * 所有学院列表
     * @return
     */
    @GetMapping("/list")
    @RequiresPermissions("sys:college:list")
    public List<SysCollegeEntity> list(){
        List<SysCollegeEntity> collegeList = sysCollegeService.list();
        for(SysCollegeEntity sysCollegeEntity : collegeList){
            SysCollegeEntity parentCollegeEntity = sysCollegeService.getById(sysCollegeEntity.getParentId());
            if(parentCollegeEntity != null){
                sysCollegeEntity.setParentName(parentCollegeEntity.getName());
            }
        }
        return collegeList;
    }

    /**
     * 新增学院信息
     * @param college
     * @return
     */
    @SysLog("新增学院")
    @PostMapping("/save")
    @RequiresPermissions("sys:college:save")
    public R save(@RequestBody SysCollegeEntity college){
        //数据校验
        verifyForm(college);

        sysCollegeService.save(college);

        return R.ok();
    }

    /**
     * 修改
     */
    @SysLog("修改学院")
    @PostMapping("/update")
    @RequiresPermissions("sys:college:update")
    public R update(@RequestBody SysCollegeEntity college){
        //数据校验
        verifyForm(college);
        sysCollegeService.updateById(college);

        return R.ok();
    }

    /**
     * 学院信息
     */
    @GetMapping("/info/{collegeId}")
    @RequiresPermissions("sys:college:info")
    public R info(@PathVariable("collegeId") Long collegeId){
        SysCollegeEntity college = sysCollegeService.getById(collegeId);
        return R.ok().put("college", college);
    }

    /**
     * 删除学院信息
     * @param collegeIds
     * @return
     */
    @SysLog("删除学院信息")
    @PostMapping("/delete")
    @RequiresPermissions("sys:college:delete")
    public R delete(@RequestBody Long[] collegeIds){
        //判断学院下是否有子学院
        for (long collegeId : collegeIds){
            List<SysCollegeEntity> collegeList = sysCollegeService.queryListParentId(collegeId);
            if(collegeList.size() > 0){
                return R.error("请先删除子学院");
            }
        }
        sysCollegeService.deleteCollege(collegeIds);

        return R.ok();
    }

    /**
     * 选择学院(添加、修改学院)
     * @return
     */
    @GetMapping("/select")
    @RequiresPermissions("sys:college:select")
    public R select(){
        //查询列表数据
        List<SysCollegeEntity> collegeList = sysCollegeService.queryListCollege();
        //添加一级学院
        SysCollegeEntity root = new SysCollegeEntity();
        root.setCollegeId(0L);
        root.setLevel(0);
        root.setName("root");
        root.setParentId(-1L);
        root.setOpen(true);
        collegeList.add(root);

        return R.ok().put("collegeList", collegeList);
    }

    /**
     * 验证参数是否正确
     * @param college
     */
    private void verifyForm(SysCollegeEntity college){
        if(StringUtils.isBlank(college.getName())){
            throw new RRException("学院名称不能为空");
        }

        if(college.getParentId() == null){
            throw new RRException("上级学院不能为空");
        }

        //上级学院级别
        int parentLevel = Constant.CollegeLevel.ZIRO.getValue();
        if(college.getParentId() != 0){
            //父级学院不是‘一级学院’时，获取父级学院信息
            SysCollegeEntity parentCollege = sysCollegeService.getById(college.getParentId());
            parentLevel = parentCollege.getLevel();
        }
        //设置当前学院级别
        college.setLevel(college.getLevel() + 1);

        //当前学院为一级学院时，上级学院只能为root
        if (college.getLevel() == Constant.CollegeLevel.FIRST.getValue()) {
            if (parentLevel != Constant.CollegeLevel.ZIRO.getValue()) {
                throw new RRException("上级学院只能为Root");
            }
            return ;
        }

        //当前学院为二级学院时，上级学院只能为一级学院
        if (college.getLevel() == Constant.CollegeLevel.SECONDARY.getValue()) {
            if (parentLevel != Constant.CollegeLevel.FIRST.getValue()) {
                throw new RRException("上级学院只能为一级学院");
            }
            return ;
        }

        //当前学院为专业时，上级学院只能为二级学院
        if (college.getLevel() == Constant.CollegeLevel.PROFESSION.getValue()) {
            if (parentLevel != Constant.CollegeLevel.SECONDARY.getValue()) {
                throw new RRException("上级学院只能为二级学院");
            }
            return ;
        }
    }
}
